package com.sikaryofficial.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sikaryofficial.backend.domain.dto.resp.ProtocolDTO;
import com.sikaryofficial.backend.domain.entity.UserProtocol;
import com.sikaryofficial.backend.domain.mapping.ProtocolMapping;
import com.sikaryofficial.backend.mapper.UserProtocolMapper;
import com.sikaryofficial.backend.service.IUserProtocolService;
import com.sikaryofficial.common.core.exception.ServiceException;
import com.sikaryofficial.common.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.MessageFormat;
import java.util.Date;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import static com.sikaryofficial.backend.constant.RedisCacheKey.PROTOCOL_TYPE_DATA_CACHE;

/**
 * 协议Service业务层处理
 *
 * @author qinjinyuan
 * @date 2023-11-16
 */
@Service
public class UserProtocolServiceImpl extends ServiceImpl<UserProtocolMapper, UserProtocol> implements IUserProtocolService {
    @Autowired
    private UserProtocolMapper protocolMapper;

    // 缓存
    @Autowired
    private RedisService redisService;

    /**
     * @param protocolType
     * @return
     */
    @Override
    public ProtocolDTO getInfoByType(Integer protocolType) {
        String cacheKey = MessageFormat.format(PROTOCOL_TYPE_DATA_CACHE, protocolType);
        ProtocolDTO protocolCacheData = redisService.getCacheObject(cacheKey);
        if (Objects.nonNull(redisService.getCacheObject(cacheKey))) {
            return protocolCacheData;
        }
        UserProtocol protocol = protocolMapper.selectOne(new LambdaQueryWrapper<UserProtocol>()
                .eq(UserProtocol::getProtocolType, protocolType)
                .eq(UserProtocol::getDeletedVersion, 0L)
                .last(" limit 1 ")
        );
        if (Objects.isNull(protocol)) {
            redisService.setCacheObject(cacheKey, new ProtocolDTO(), 1L, TimeUnit.SECONDS);
            return new ProtocolDTO();
        }
        ProtocolDTO protocolDTO = ProtocolMapping.INSTANCE.coverToResp(protocol);
        redisService.setCacheObject(cacheKey, protocolDTO, 1L, TimeUnit.HOURS);
        return protocolDTO;
    }

    /**
     * 更新协议信息
     *
     * @param protocol 协议信息
     * @return 是否更新成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateProtocol(ProtocolDTO protocol) {
        if (Objects.isNull(protocol.getProtocolType())) {
            throw new ServiceException("协议类型不能为空");
        }
        if (Objects.isNull(protocol.getContent())) {
            throw new ServiceException("协议内容不能为空");
        }
        if (protocol.getProtocolType() != 1 || protocol.getProtocolType() != 2) {
            throw new ServiceException("协议类型错误");
        }
        // 查询协议
        UserProtocol userProtocol = protocolMapper.selectOne(new LambdaQueryWrapper<UserProtocol>()
                .eq(UserProtocol::getProtocolType, protocol.getProtocolType())
                .eq(UserProtocol::getProtocolId,protocol.getProtocolId())
                .eq(UserProtocol::getDeletedVersion, 0L)
                .last(" limit 1 ")
        );
        // 更新协议
        this.update(new LambdaUpdateWrapper<UserProtocol>()
                .set(UserProtocol::getContent, protocol.getContent())
                .set(UserProtocol::getUpdatedTime, new Date())
                .eq(UserProtocol::getProtocolId, userProtocol.getProtocolId())
                .eq(UserProtocol::getProtocolId,protocol.getProtocolId())
                .eq(UserProtocol::getDeletedVersion, 0L));
        String cacheKey = MessageFormat.format(PROTOCOL_TYPE_DATA_CACHE, protocol.getProtocolType());
        // 清除缓存
        return redisService.deleteObject(cacheKey);
    }
}
